package com.xja.web.domain.controllers;

import com.github.wxpay.sdk.WXPay;
import com.xja.web.domain.commons.MyConfig;
import com.xja.web.domain.service.OrderService;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("PayController")
public class PayController {
    /**
     * 回调的url 用来接收并回复微信支付平台信息的
     */
    @Autowired
    private OrderService orderService;
    @RequestMapping("notify")
    public String notifyUrl(HttpServletRequest request){
        try {
            ServletInputStream inputStream = request.getInputStream();
            Element rootElement = new SAXBuilder().build(inputStream).getRootElement();
            String out_trade_no = rootElement.getChildText("out_trade_no");
            orderService.updateOrder(out_trade_no);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JDOMException e) {
            e.printStackTrace();
        }

        return "<xml>\n" +
                "  <return_code><![CDATA[SUCCESS]]></return_code>\n" +
                "  <return_msg><![CDATA[OK]]></return_msg>\n" +
                "</xml>";
    }

}
